package cinema;
import java.util.Random;
import cinema.CinemaShowTime;
public class CinemaHall {
    private Random random = new Random();
    private int hallNum;
    private int rowCount;
    private int colCount;
    private boolean[][] seats;
    private int soldSeats;
    private CinemaShowTime.Cinema cinema;
    public CinemaHall(String hallNum, int rowCount, int colCount) {
        this.rowCount = rowCount;
        this.colCount = colCount;
        this.seats = new boolean[rowCount][colCount];
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                seats[i][j] = true;
            }
        }
        this.soldSeats=0;
    }

    public String generateSeatId(int row, int col) {
        if (row < 0 || row >= rowCount || col < 0 || col >= colCount) {
            System.out.println("座位不存在，行：" + row + "，列：" + col);
            return null;
        }
        int randomNum = random.nextInt(10000);
        String randomId = String.format("%05d", randomNum);
        return ("行：" + row + "列：" + col + ",ID：" + randomId);
    }

    public boolean isSeatSold(int row, int col) {
        if (row < 0 || row >= rowCount || col < 0 || col >= colCount) {
            System.out.println("位置不存在");
            return false;
        }
        return !seats[row][col];
    }

    public void sellSeat(int row, int col) {
        if (row >= 0 && row <= rowCount && col >= 0 && col <= colCount) {
            if (seats[row][col]) {
                seats[row][col]=false;
                soldSeats++;
            }else {
                System.out.println("想买的位置已售,无法购买，行：" + row + "列:" + col);
            }
        } else {
            System.out.println("想买的位置不存在，行："+row+"列："+col);
        }
    }

    public String buyTicket(int row, int col) {
        // 1. 校验座位合法性
        if (row < 0 || row >= rowCount || col < 0 || col >= colCount) {
            return "座位位置无效！行：" + row + " 列：" + col;
        }

        // 2. 校验座位是否已售
        if (isSeatSold(row, col)) {
            return "座位已售罄！行：" + row + " 列：" + col;
        }

        // 3. 执行售票（标记为已售）
        sellSeat(row, col);

        // 4. 生成座位凭证
        String seatIdInfo = generateSeatId(row, col);

        // 5. 拼接并打印完整电影票
        String ticketInfo = "\n======== 电影票 ========\n" +
                "\n" +
                "影片名称：" + getMovieName() + "\n" +
                "影厅编号：" + hallNum + "\n" +
                "座位信息：" + seatIdInfo + "\n" +
                "========================";
        System.out.println(ticketInfo); // 直接打印电影票

        return seatIdInfo; // 返回座位凭证（也可根据需求返回完整 ticketInfo）
    }
    public void printSeatTable() {
        System.out.println("影厅" + hallNum + "座位表：");
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                System.out.print(seats[i][j] ? " 0 " : " 1 ");

            }
            System.out.println();
        }
    }
    public int getRemainingSeats(){
        return rowCount*colCount-soldSeats;
    }
    public String getMovieName(){
        return cinema.movieName;
    }


    //返回true说明不可以售出，false表能出售
    public static void main(String[] args) {
        CinemaHall hall = new CinemaHall("3", 3, 5);
        hall.printSeatTable();
        System.out.println("测试3号厅3行5列：");
        System.out.println("\n    测试座位ID生成    ");
        String id1 = hall.generateSeatId(0, 0);
        System.out.println("座位(0,0)的ID：" + id1);

        String id2 = hall.generateSeatId(2, 4);
        System.out.println("座位(2,4)的ID：" + id2);

        hall.generateSeatId(3, 2);
        hall.generateSeatId(1, 5);

        System.out.println("座位(1,2)能否购买：" + hall.isSeatSold(1, 2));

        hall.sellSeat(1, 2);
        System.out.println("座位(1,2)能否购买：" + hall.isSeatSold(1, 2));
        hall.printSeatTable();

    }
}